import { App } from 'vue'
import XEUtils from 'xe-utils'
import {
  VXETable,
  Icon,
  Column,
  Colgroup,
  Pager,
  Table,
  Toolbar,
  Button,
  RadioButton,
  RadioGroup,
  Input,
  Tooltip,
  Checkbox
} from 'vxe-table'
import 'vxe-table/lib/style.css'
import zhCNLocat from 'vxe-table/lib/locale/lang/zh-CN'
import http from './http'

// 全局默认参数
VXETable.config({
  version: 0,
  zIndex: 100,
  table: {
    autoResize: true,
    minHeight: 180
  }
})

// 导入默认的国际化（如果项目中使用多语言，则应该导入到 vue-i18n 中）
VXETable.config({
  i18n: (key, args) => XEUtils.toFormatString(XEUtils.get(zhCNLocat, key), args)
})

interface QueryConfig {
  getConfigUrl: string
  saveConfigUrl: string
  apiPath: string
}

const queryConfig: QueryConfig = {
  getConfigUrl: '',
  saveConfigUrl: '',
  apiPath: ''
}
export function useTable(app: App, option: QueryConfig) {
  app
    .use(Icon)
    .use(Column)
    .use(Colgroup)
    .use(Table)
    .use(Pager)
    .use(Toolbar)
    .use(Button)
    .use(RadioButton)
    .use(RadioGroup)
    .use(Input)
    .use(Tooltip)
    .use(Checkbox)
  Object.assign(queryConfig, option)
}

// 切换全屏
export const toggleFullScreen = () => {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen()
  } else if (document.exitFullscreen) {
    document.exitFullscreen()
  }
}

// 获取用户配置
export const getUserConfig = (data) => {
  Object.assign(data, { settingType: 2 })
  return http({ url: `${queryConfig.apiPath}${queryConfig.getConfigUrl}`, method: 'get', data })
}

// 保存用户配置
export const saveUserConfig = (data) => {
  Object.assign(data, { settingType: 2 })
  return http({ url: `${queryConfig.apiPath}${queryConfig.saveConfigUrl}`, method: 'post', data })
}
